package com.ehotting.mqbd.context.listener;

import com.ehotting.eaf.core.utils.OkHttpUtils;
import com.ehotting.eaf.core.utils.ip.LocalIPHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@Configuration
@ConfigurationProperties
public class ApplicationContextListener4Http implements CommandLineRunner, DisposableBean {

    private static Logger logger = LoggerFactory.getLogger(ApplicationContextListener4Http.class);


    @Autowired
    private LocalIPHelper localIPHelper;
    @Value("${server.port}")
    private String port;
    @Value("${module.name}")
    private String module;
    @Value("${module.login_url:http://10.21.31.33:38586/module/login}")
    private String moduleLoginUrl;
    @Value("${module.logout_url:http://10.21.31.33:38586/module/logout}")
    private String moduleLogoutUrl;

    private volatile boolean isRun = true;

    private Thread thread = null;





    @Override
    public void run(String... args)  {
        logger.info("Start run");

        Runnable run = new Runnable() {
            @Override
            public void run() {
                while (isRun){
                    String ip = localIPHelper.getIp();
                    logger.info("module[{}],ip[{}],port[{}]",module,ip,port);
                    Map<String,String> map = new HashMap<String,String>();
                    map.put("module",module);
                    map.put("ip",ip);
                    map.put("port",port);
                    String result = OkHttpUtils.builder().url(moduleLoginUrl).addParam(map).post(true).async();
                    logger.info(result);
                    try {
                        Thread.sleep(1000*60*60);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        };
        thread = new Thread(run);
        thread.start();

        logger.info("Finish run");
    }

    @Override
    public void destroy() throws Exception {
        logger.info("Start destroy");

        interruptRegister();

        String ip = localIPHelper.getIp();
        logger.info("module[{}],ip[{}],port[{}]",module,ip);
        Map<String,String> map = new HashMap<String,String>();
        map.put("module",module);
        map.put("ip",ip);
        map.put("port",port);
        String result = OkHttpUtils.builder().url(moduleLogoutUrl).addParam(map).post(true).async();
        logger.info(result);
        logger.info("Finish destroy");
    }



    private void interruptRegister(){
        isRun = false;
        if(thread!=null){
            while(thread.isAlive() && !thread.isInterrupted() ){
                thread.interrupt();
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}